﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Indie_Game_Contest
{
    class Platform
    {
        private List<int> m_IDs = new List<int>();
        private Vector2 m_vPlatform = Vector2.Zero;
        private Vector2 m_vVelocity = Vector2.Zero;
        private Vector2 m_vMinLimit = Vector2.Zero;
        private Vector2 m_vMaxLimit = Vector2.Zero;
        private bool m_bNegateVelocityX = false;
        private bool m_bNegateVelocityY = false;
        private bool m_bPlayerOnPlatform = false;

        public void AddTile( Tile tiles, bool bCollideable = true )
        {
           tiles.Collision = bCollideable;
           m_IDs.Add( ObjectManager.Instance.AddTile( tiles ) );
        }


        public void UpdatePlatform(float dt)
        {
            Vector2 vLocal = Vector2.Zero;
            for( int i = 0; i < IDs.Count; ++i )
            {
                vLocal = ObjectManager.Instance.GetTile(IDs[i]).Position;
                if (BNegateVelocityX == false)
                    vLocal.X += (VVelocity.X * dt);
                else
                    vLocal.X += (-VVelocity.X * dt);

                if (BNegateVelocityY == false)
                    vLocal.Y += (VVelocity.Y * dt);
                else
                    vLocal.Y += (-VVelocity.Y * dt);

                ObjectManager.Instance.GetTile(IDs[i]).Position = vLocal;
            }

            if (vLocal.X > VMaxLimit.X )
                BNegateVelocityX = true;
            else if( vLocal.X < VMinLimit.X )
                BNegateVelocityX = false;

            if (vLocal.Y > VMaxLimit.Y)
                BNegateVelocityY = true;
            else if (vLocal.Y < VMinLimit.Y)
                BNegateVelocityY = false;

            if (BPlayerOnPlatform == true)
            {
                vLocal = GameplayState.Instance.Player.Position;
                if (BNegateVelocityX == false)
                    vLocal.X += (VVelocity.X * dt);
                else
                    vLocal.X += (-VVelocity.X * dt);

                if (BNegateVelocityY == false)
                    vLocal.Y += (VVelocity.Y * dt);
                else
                    vLocal.Y += (-VVelocity.Y * dt);

                GameplayState.Instance.Player.Position = vLocal;

            }

            BPlayerOnPlatform = false;
        }


        public List<int> IDs
        {
             get { return m_IDs; }
            set { m_IDs = value; }
        }

        public Vector2 VPlatform
        {
            get { return m_vPlatform; }
            set { m_vPlatform = value; }
        }

        public Vector2 VVelocity
        {
            get { return m_vVelocity; }
            set { m_vVelocity = value; }
        }

        public Vector2 VMinLimit
        {
            get { return m_vMinLimit; }
            set { m_vMinLimit = value; }
        }

        public Vector2 VMaxLimit
        {
            get { return m_vMaxLimit; }
            set { m_vMaxLimit = value; }
        }

        public bool BPlayerOnPlatform
        {
            get { return m_bPlayerOnPlatform; }
            set { m_bPlayerOnPlatform = value; }
        }

        public bool BNegateVelocityX
        {
            get { return m_bNegateVelocityX; }
            set { m_bNegateVelocityX = value; }
        }

        public bool BNegateVelocityY
        {
            get { return m_bNegateVelocityY; }
            set { m_bNegateVelocityY = value; }
        }

    }
}
